package com.hsh;

import com.hsh.config.ConfigManager;
import com.hsh.db.CFManager;
import com.hsh.db.RDB;
import org.rocksdb.*;

import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.TimeUnit;


public class TestDB {
    private static String dbPath = "/Users/hsh/rocks_db";
    private static String configPath = "/apps/code/oschina/learingcode/rocksdb/src/main/resources/chronos.yaml";

    public static void init() {
        ConfigManager.initConfig(configPath);
        RDB.init(dbPath);
    }

    private static final long MIN_TIMESTAMP = 0000000000;

    private static final long ONE_DAY = 24 * 60 * 60 * 1000; //ms

    public static void main(String[] args) throws RocksDBException {
        test();
    }

    public static void testRandomWrite() {
        System.out.println(System.currentTimeMillis() / 1000);
    }

    public static void test() throws RocksDBException {
        HashMap<Integer, Object> numbers = new HashMap<>();
        init();
        WriteBatch wb = new WriteBatch();
        ColumnFamilyHandle cfHandler = CFManager.CFH_DEFAULT;
//        RDB.deleteFilesInRange(cfHandler, "0".getBytes(), "91".getBytes());
        long st = System.currentTimeMillis();

        Random random = new Random();
        for (int i = 0; i < 1000000; i++) {
            int num = random.nextInt(100000000);
            while (numbers.get(num) != null) {
                num = random.nextInt();
            }
            numbers.put(num, "");
            wb.put(cfHandler, (num + "-random").getBytes(), "test1111111".getBytes());

            if (i % 300 == 0) {
                RDB.writeAsync(wb);
                wb.clear();
            }
        }
        RDB.writeAsync(wb);

        long ed = System.currentTimeMillis();

        RocksIterator it = RDB.newIterator(cfHandler);
        int count = 0;
        for (it.seekToFirst(); it.isValid(); it.next()) {
            count++;
            if (count < 10) {
                System.out.println(new String(it.key()) + "  value : " + new String(it.value()));
            }
        }
        System.out.println("write cost :" + (ed - st));
        System.out.println("count :" + count);
        it.close();
    }

    public static void t1() {
        init();
        WriteBatch wb = new WriteBatch();
        ColumnFamilyHandle cfHandle = CFManager.CFH_DEFAULT;
        RDB.deleteFilesInRange(CFManager.CFH_DEFAULT, "0".getBytes(), "1324356529".getBytes());
        long st = System.currentTimeMillis();
        for (int i = 1; i < 31; i++) {
            wb.put(cfHandle, (i + "-5-5-345-356-234-232").getBytes(), "tasdfasdgasdfestfordb1111111".getBytes());

            if (i % 30 == 0) {
                RDB.writeAsync(wb);
                wb.clear();
            }
        }
        RDB.writeAsync(wb);

        long ed = System.currentTimeMillis();
        System.out.println("write cost :" + (ed - st));

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        long start = System.currentTimeMillis();
        RocksIterator it = RDB.newIterator(cfHandle);
        byte[] now = "1".getBytes();
        long count = 0;
        for (it.seekToFirst(); it.isValid(); it.next()) {
            System.out.println(new String(it.key()) + " " + new String(it.value()));
            count++;
        }
        it.close();
        long end = System.currentTimeMillis();
        System.out.println("cost : " + (end - start) + " count:" + count);

    }

    public static void testDeleteRange() {
        init();
        WriteBatch wb = new WriteBatch();
        ColumnFamilyHandle cfHandle = CFManager.CFH_DEFAULT;

        long st = System.currentTimeMillis();
        for (int i = 100000; i < 200000; i++) {
            wb.put(cfHandle, ("1324356527-" + i + "-5-5-345-356-234-232").getBytes(), "tasdfasdgasdfestfordb".getBytes());

            if (i % 30 == 0) {
                RDB.writeAsync(wb);
                wb.clear();
            }
        }
        for (int i = 100000; i < 200000; i++) {
            wb.put(cfHandle, ("1324356525-" + i + "-5-5-345-356-234-232").getBytes(), "tasdfasdgasdfestfordb".getBytes());

            if (i % 30 == 0) {
                RDB.writeAsync(wb);
                wb.clear();
            }
        }
        for (int i = 100000; i < 200000; i++) {
            wb.put(cfHandle, ("1324356529-" + i + "-5-5-345-356-234-232").getBytes(), "tasdfasdgasdfestfordb".getBytes());

            if (i % 30 == 0) {
                RDB.writeAsync(wb);
                wb.clear();
            }
        }
        RDB.writeAsync(wb);

        long ed = System.currentTimeMillis();
        System.out.println("write cost :" + (ed - st));

        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        long start = System.currentTimeMillis();
        RocksIterator it = RDB.newIterator(cfHandle);
        byte[] now = "1324356527".getBytes();
        long count = 0;
        for (it.seek(now); it.isValid(); it.next()) {
            System.out.println(new String(it.key()) + " " + new String(it.value()));
            count++;
            if (count == 100000)
                break;
        }
        it.close();
        long end = System.currentTimeMillis();
        System.out.println("cost : " + (end - start) + " count:" + count);
        RDB.deleteFilesInRange(CFManager.CFH_DEFAULT, "132435653".getBytes(), "1324356529".getBytes());

        count = 0;
        it = RDB.newIterator(cfHandle);
        now = "1324356529".getBytes();
        for (it.seek(now); it.isValid(); it.next()) {
//            System.out.println(new String(it.key()) + " " + new String(it.value()));
            count++;
//            if (count == 100000)
//                break;
        }
        it.close();
        end = System.currentTimeMillis();
        System.out.println("cost : " + (end - start) + " count:" + count);

        destructor();
    }

    public static void destructor() {
        RDB.close();
    }
}